package com.bw.lib_storage.mmkv

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * ReadWriteProperty：属性委托
 */
class MMKVProperty<T>(private val defValue:T):ReadWriteProperty<MMKVOwner,T> {
    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T {
//        when(defValue){
//            is String -> return thisRef.mmkv.getString(property.name,defValue) as T
//            is Int -> return thisRef.mmkv.getInt(property.name,defValue) as T
//            is Boolean -> return thisRef.mmkv.getBoolean(property.name,defValue) as T
//            else -> return  Unit as T
//        }
        return thisRef.mmkv.run {
            when(defValue){
                is String -> getString(property.name,defValue)
                is Int -> getInt(property.name,defValue)
                is Boolean -> getBoolean(property.name,defValue)
                is Float -> getFloat(property.name,defValue)
                is Long -> getLong(property.name,defValue)
                is Double -> decodeDouble(property.name,defValue)
                else -> Unit
            } as T
        }
    }

    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T) {
        thisRef.mmkv.run {
            when(value){
                is String -> putString(property.name,value)
                is Int -> putInt(property.name,value)
                is Boolean -> putBoolean(property.name,value)
                is Float -> putFloat(property.name,value)
                is Long -> putLong(property.name,value)
                is Double -> encode(property.name,value)
                else -> Unit
            }
        }
    }
}